home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11788 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: String Problem
  5. Date: 25 Mar 1996 15:21:41 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4j79q5INNt27@keats.ugrad.cs.ubc.ca>
  8. References: <4j6l61$4no@B1FF.mindspring.com> <Pine.A32.3.91.960325141646.16066F-100000@red.weeg.uiowa.edu>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <Pine.A32.3.91.960325141646.16066F-100000@red.weeg.uiowa.edu>,
  12. The Amorphous Mass  <robinson@blue.weeg.uiowa.edu> wrote:
  13.  >On Mon, 25 Mar 1996, Shon Frazier wrote:
  14.  >
  15.  >> This is supposed to be a variation on sample code from a textbook.
  16.  >> Can anyone tell me why NameCheck = 0 when I run the program?
  17.  >
  18.  >  You can't compare strings with == (the code with compare the 
  19.  >values of the pointers to those strings, which is an altogether different 
  20.  >matter).  You need to use the standard library function strcmp() (in 
  21.  ><string.h>) which returns 0 if the strings are equal.
  22.  >
  23.  >  int strcmp(const char *s1, const char *s2);
  24.  >
  25.  >  Strings are not a basic data type in C, so it is generally true that if 
  26.  >you need to do something to a string, you pass it to a function. :-)
  27.  
  28. Nevertheless, the ANSI standard blesses the standard library functions as
  29. special, in effect raising them to the status of operators of the language.
  30. Compilers have the license (in a hosted implementation) to turn strcmp() into
  31. inline code, and many compilers, including GCC, take advantage of this.
  32.  
  33. In essence, strcmp(string1,string2) becomes a syntactic variant of something
  34. like (string1 != string2), barring the three-way result semantics.
  35.  
  36. You can still think of strcmp() as being a function, of course. The standard
  37. guarantees that you can take the address of any standard library
  38. function, so that you can pass a pointer to strcmp to a sorting function like
  39. qsort(). This is very nice compared to languages like Pascal whose built-in
  40. operators like writeln() can't be, for the most part, written in the language
  41. itself.
  42. -- 
  43.  
  44.